package com.fortune.service.impl;

import com.fortune.constants.Constants;
import com.fortune.constants.RoleConstants;
import com.fortune.constants.enums.FilePaths;
import com.fortune.dao.MPermissionDao;
import com.fortune.dao.PMAccountMgtDao;
import com.fortune.dto.in.pMAccountMgt.PMAccountAddDto;
import com.fortune.dto.in.pMAccountMgt.PMAccountEditDto;
import com.fortune.dto.in.pMAccountMgt.PMAccountListDto;
import com.fortune.dto.in.pMAccountMgt.PMUserUpAndDownDto;
import com.fortune.entity.MPermission;
import com.fortune.service.PMAccountMgtService;
import com.fortune.util.UploadUtil;
import com.fortune.vo.pMAccountMgt.PMAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Service
public class PMAccountMgtServiceImpl implements PMAccountMgtService {
    @Autowired
    private PMAccountMgtDao pmAccountMgtDao;
    @Autowired
    private MPermissionDao mPermissionDao;

    /**
     * 厂商列表
     * @param pmAccountListDto
     * @return
     */
    @Override
    public List<PMAccount> queryAccountList(PMAccountListDto pmAccountListDto) {
        return pmAccountMgtDao.queryAccountList(pmAccountListDto);
    }

    /**
     * 厂商列表个数
     * @param pmAccountListDto
     * @return
     */
    @Override
    public int queryAccountListCount(PMAccountListDto pmAccountListDto) {
        return pmAccountMgtDao.queryAccountListCount(pmAccountListDto);
    }

    /**
     * 检测是否存在
     * @param pmAccountAddDto
     * @return
     */
    @Override
    public int queryAccountIsExist(PMAccountAddDto pmAccountAddDto) {
        return pmAccountMgtDao.queryAccountIsExist(pmAccountAddDto);
    }

    /**
     * 检测是否存在
     * @param phone
     * @return
     */
    @Override
    public int queryAccountByNumber(String phone) {
        return pmAccountMgtDao.queryAccountByNumber(phone);
    }

    /**
     * 添加厂商
     * @param pmAccountAddDto
     * @return
     */
    @Override
    @Transactional
    public int addAccount(PMAccountAddDto pmAccountAddDto) {
        String companyId = UUID.randomUUID().toString().replace("-","");
        MultipartFile pic = pmAccountAddDto.getImgUrl();
        String url = UploadUtil.saveFile(pic, FilePaths.COMPANYLOG);
        int num = pmAccountMgtDao.addAccount(companyId,pmAccountAddDto,url);
        if (num < 1){
            throw new RuntimeException("添加厂商失败");
        }
        String password = Constants.M_USER_PASSWORD;
        String adminId = RoleConstants.SUPER_ADMIN_ID;
        num = pmAccountMgtDao.addMUserInfo(adminId,companyId,pmAccountAddDto,password);
        if (num < 1){
            throw new RuntimeException("添加厂商失败");
        }
        String adminName = RoleConstants.SUPER_ADMIN_NAME;
        num = pmAccountMgtDao.addMPosition(companyId,adminId,adminName);
        if(num < 1){
            throw new RuntimeException("添加厂商失败");
        }
        List<MPermission> mPermissionList = mPermissionDao.queryPermissionList();
        if (mPermissionList != null && mPermissionList.size() > 0){
            num = mPermissionDao.addMPositionPermission(companyId,adminId,mPermissionList);
            if(num < 1){
                throw new RuntimeException("添加厂商失败");
            }
        }
        return num;
    }

    /**
     * 检测是否存在
     * @param id
     * @return
     */
    @Override
    public int queryAccountById(String id) {
        return pmAccountMgtDao.queryAccountById(id);
    }

    /**
     * 检测是否存在
     * @param pmAccountEditDto
     * @return
     */
    @Override
    public int queryEditAccountIsExist(PMAccountEditDto pmAccountEditDto) {
        return pmAccountMgtDao.queryEditAccountIsExist(pmAccountEditDto);
    }

    /**
     * 编辑厂商
     * @param pmAccountEditDto
     * @return
     */
    @Override
    public int editAccountCompanyId(PMAccountEditDto pmAccountEditDto) {
        String phone = pmAccountMgtDao.queryAccountPhoneById(pmAccountEditDto.getId());
        int num = 0;
        if (pmAccountEditDto.getImgUrl()!=null && !pmAccountEditDto.getImgUrl().getOriginalFilename().equals("")){
            MultipartFile pic = pmAccountEditDto.getImgUrl();
            String url = UploadUtil.saveFile(pic, FilePaths.COMPANYLOG);
            num = pmAccountMgtDao.editAccountUrl(pmAccountEditDto,url);
        }else {
            num = pmAccountMgtDao.editAccount(pmAccountEditDto);
        }

        if (num < 1){
            throw new RuntimeException("编辑失败");
        }
        String password = Constants.M_USER_PASSWORD;
        num = pmAccountMgtDao.editMUser(password,pmAccountEditDto);
        if (num < 1){
            throw new RuntimeException("编辑失败");
        }
        return num;
    }

    /**
     * 删除经销商
     * @param acccountId
     * @return
     */
    @Override
    public int deleteAccount(String acccountId) {
        return pmAccountMgtDao.deleteAccount(acccountId);
    }

    /**
     * 重置厂商密码
     * @param acccountId
     * @param password
     * @return
     */
    @Override
    public int resetPassword(String acccountId, String password) {
        return pmAccountMgtDao.resetMPassword(acccountId,password);
    }

    /**
     * 查询厂商信息
     * @param id
     * @return
     */
    @Override
    public PMAccount queryAccountInfoById(String id) {
        return pmAccountMgtDao.queryAccountInfoById(id);
    }

    /**
     * 修改状态
     * @param userUpAndDownDto
     * @return
     */
    @Override
    public int upAndDownUser(PMUserUpAndDownDto userUpAndDownDto) {
        return pmAccountMgtDao.upAndDownUser(userUpAndDownDto);
    }

    @Override
    public int queryAccountIsExistByName(PMAccountAddDto pmAccountAddDto) {
        return pmAccountMgtDao.queryAccountIsExistByName(pmAccountAddDto);
    }

    @Override
    public int queryAccountIsExistByAbbreviation(PMAccountAddDto pmAccountAddDto) {
        return pmAccountMgtDao.queryAccountIsExistByAbbreviation(pmAccountAddDto);
    }

    @Override
    public int queryAccountIsExistByNumber(PMAccountAddDto pmAccountAddDto) {
        return pmAccountMgtDao.queryAccountIsExistByNumber(pmAccountAddDto);
    }

    @Override
    public int queryEditAccountIsExistByName(PMAccountEditDto pmAccountEditDto) {
        return pmAccountMgtDao.queryEditAccountIsExistByName(pmAccountEditDto);
    }

    @Override
    public int queryEditAccountIsExistByAbbreviation(PMAccountEditDto pmAccountEditDto) {
        return pmAccountMgtDao.queryEditAccountIsExistByAbbreviation(pmAccountEditDto);
    }

    @Override
    public int queryEditAccountIsExistByNumber(PMAccountEditDto pmAccountEditDto) {
        return pmAccountMgtDao.queryEditAccountIsExistByNumber(pmAccountEditDto);
    }
}
